from typing import List


class Solution:
    def getIntersectionNode(self, headA, headB):
        p, q = headA, headB
        while headA is not headB:
            headA = headA.next if headA else q
            headB = headB.next if headB else p

        return headA

class Listnode:
    def __init__(self, data):
        self.val = data # data
        self.next = None # pointer

# 接收一个列表和创建好的链表和相交节点
# head, intersect_node = createLinkedList([1, 9, 1, 2], skip=3)
def createLinkedList(nums: List, skip):
    # 处理空列表
    if not nums:
        return None

    # 创建头节点(整体的头，先用head记住便于solution中调用)
    head = Listnode(nums[0])
    current = head
    node_list = [head]

    # 遍历剩余元素，创建后续节点
    for i in nums[1:]:
        current.next = Listnode(i)
        current = current.next
        node_list.append(current.next)

    # 判断skip合法性
    if skip is not None and skip >= 0 and skip < len(nums):
        return head, node_list[skip]
    else:
        return head, None






